const Error = require('./error')
const storage = require('./storage.js')
const request = require('./request')

const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const deviceCheck = class deviceCheck {

    constructor() {}

    get(id) {
        let params = {
            id: id,
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.GET_DEVICE_CHECK.url,
                MS._config.parameters.DEVICE_CHECK.GET_DEVICE_CHECK.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10,
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.moId) && isString(options.moId)) {
                params.moId = options.moId;
            }
            if(!isUndefined(options.deviceName) && isString(options.deviceName)) {
                params.deviceName = options.deviceName;
            }
            if(!isUndefined(options.applicantId) && isString(options.applicantId)) {
                params.applicantId = options.applicantId;
            }
            if(!isUndefined(options.categoryId) && isString(options.categoryId)) {
                params.categoryId = options.categoryId;
            }
            if(!isUndefined(options.status) && isNumber(options.status)) {
                params.status = options.status;
            }
            if(!isUndefined(options.statusArray) && isArray(options.statusArray)) {
                params.statusArray = JSON.stringify(options.statusArray);
            }
            if(!isUndefined(options.deviceId) && isString(options.deviceId)) {
                params.deviceId = options.deviceId;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.LOAD_DEVICE_CHECK.url,
                MS._config.parameters.DEVICE_CHECK.LOAD_DEVICE_CHECK.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadDeviceCheckList(checkId) {
        let params = {
            id: checkId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.LOAD_DEVICE_CHECK_LIST.url,
                MS._config.parameters.DEVICE_CHECK.LOAD_DEVICE_CHECK_LIST.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    applyCheck(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.moId) && isString(options.moId)) {
                params.moId = options.moId;
            }
            if(!isUndefined(options.moName) && isString(options.moName)) {
                params.moName = options.moName;
            }
            if(!isUndefined(options.deviceName) && isString(options.deviceName)) {
                params.deviceName = options.deviceName;
            }
            if(!isUndefined(options.deviceId) && isString(options.deviceId)) {
                params.deviceId = options.deviceId;
            }
            if(!isUndefined(options.picture) && isString(options.picture)) {
                params.picture = options.picture;
            }
            if(!isUndefined(options.remark) && isString(options.remark)) {
                params.remark = options.remark;
            }
            if(!isUndefined(options.applicantId) && isString(options.applicantId)) {
                params.applicantId = options.applicantId;
            }
            if(!isUndefined(options.applicantName) && isString(options.applicantName)) {
                params.applicantName = options.applicantName;
            }
            if(!isUndefined(options.address) && isString(options.address)) {
                params.address = options.address;
            }
            if(!isUndefined(options.contacts) && isString(options.contacts)) {
                params.contacts = options.contacts;
            }
            if(!isUndefined(options.contactsPhone) && isString(options.contactsPhone)) {
                params.contactsPhone = options.contactsPhone;
            }
            if(!isUndefined(options.categoryId) && isString(options.categoryId)) {
                params.categoryId = options.categoryId;
            }
            if(!isUndefined(options.categoryName) && isString(options.categoryName)) {
                params.categoryName = options.categoryName;
            }
            if(!isUndefined(options.price) && isNumber(options.price)) {
                params.price = options.price;
            }
            if(!isUndefined(options.isShip) && isBoolean(options.isShip)) {
                params.isShip = options.isShip;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.APPLY_CHECK.url,
                MS._config.parameters.DEVICE_CHECK.APPLY_CHECK.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    paySuccess(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.payPrice) && isNumber(options.payPrice)) {
                params.payPrice = options.payPrice;
            }
            if(!isUndefined(options.payMode) && isNumber(options.payMode)) {
                params.payMode = options.payMode;
            }
            if(!isUndefined(options.payType) && isNumber(options.payType)) {
                params.payType = options.payType;
            }
            if(!isUndefined(options.otherOrderId) && isString(options.otherOrderId)) {
                params.otherOrderId = options.otherOrderId;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.PAY_SUCCESS.url,
                MS._config.parameters.DEVICE_CHECK.PAY_SUCCESS.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    distribute(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.maintainerGroupId) && isString(options.maintainerGroupId)) {
                params.maintenanceGroupId = options.maintainerGroupId;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.DISTRIBUTE_EMPLOYEE.url,
                MS._config.parameters.DEVICE_CHECK.DISTRIBUTE_EMPLOYEE.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    feedBack(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.contentStr) && isString(options.contentStr)) {
                params.contentStr = options.contentStr;
            }
            if(!isUndefined(options.orderList) && isArray(options.orderList)) {
                params.orderListJson = JSON.stringify(options.orderList)
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.FEED_BACK.url,
                MS._config.parameters.DEVICE_CHECK.FEED_BACK.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancel(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.DEVICE_CHECK.CANCEL.url,
                MS._config.parameters.DEVICE_CHECK.CANCEL.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = deviceCheck